import { isArray } from './type'

export enum Text {
    black = '\x1b[90m',
    red = '\x1b[91m',
    green = '\x1b[92m',
    yellow = '\x1b[93m',
    blue = '\x1b[94m',
    white = '\x1b[37m'
}
export enum Back {
    black = '\x1b[100m',
    red = '\x1b[101m',
    green = '\x1b[102m',
    yellow = '\x1b[103m',
    blue = '\x1b[104m',
    white = '\x1b[107m'
}
export enum Style {
    bold = '\x1b[1m', //加粗
    fade = '\x1b[2m', //暗淡
    line = '\x1b[4m', //下画线
    blink = '\x1b[5m' //闪烁
}

export type PrintType = Text | Back | Style

export const t = function (text: string, ...styles: PrintType[]) {
    if (isArray(styles) && styles.length) {
        const reset = '\x1b[0m'
        return `${styles.join('')}${text}${reset}`
    }
    return t
}

export const print = function (...messages: any[]) {
    process.stdout.write(messages.map((m) => String(m)).join(' ') + '\n')
}

export const info = (...messages: any[]) => {
    process.stdout.write(`[INFO] ${messages.map((m) => String(m)).join(' ')}` + '\n')
}

export const warn = (...messages: any[]) => {
    process.stdout.write(`[WARN] ${messages.map((m) => String(m)).join(' ')}` + '\n')
}

export const error = (...messages: any[]) => {
    process.stdout.write(`[ERROR] ${messages.map((m) => String(m)).join(' ')}` + '\n')
}

export const clearLine = () => {
    const tty = process.stdout.isTTY && !process.env.CI
    if (tty) {
        process.stdout.clearLine(0)
        process.stdout.cursorTo(0)
    }
}
